using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
    [TestFixture]
    public class Chsarp
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
        private bool acceptNextAlert = true;
        
        [SetUp]
        public void SetupTest()
        {
            driver = new FirefoxDriver();
            baseURL = "http://www.clickbank.com/";
            verificationErrors = new StringBuilder();
        }
        
        [TearDown]
        public void TeardownTest()
        {
            try
            {
                driver.Quit();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }
        
        [Test]
        public void TheChsarpTest()
        {
            driver.Navigate().GoToUrl(baseURL + "/");
            driver.FindElement(By.LinkText("Login")).Click();
            driver.FindElement(By.Id("nick")).Clear();
            driver.FindElement(By.Id("nick")).SendKeys("swiftdove");
            driver.FindElement(By.Id("pass")).Clear();
            driver.FindElement(By.Id("pass")).SendKeys("LIli1966%%");
            driver.FindElement(By.Id("login")).Click();
            driver.FindElement(By.LinkText("Marketplace")).Click();
            driver.FindElement(By.Id("includeKeywords")).Clear();
            driver.FindElement(By.Id("includeKeywords")).SendKeys("weight loss");
            driver.FindElement(By.CssSelector("input[type=\"submit\"]")).Click();
            driver.FindElement(By.CssSelector("button.cssButton.greenCssButton")).Click();
            // ERROR: Caught exception [ERROR: Unsupported command [waitForPopUp | link | 30000]]
            // ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=link | ]]
            driver.FindElement(By.Id("promocode")).Clear();
            driver.FindElement(By.Id("promocode")).SendKeys("weight-loss");
            driver.FindElement(By.Id("submit")).Click();
            driver.FindElement(By.XPath("//input[@value='<a href=\"http://da185r13smlm-aeqzchmvby7ot.hop.clickbank.net/?tid=WEIGHT-LOSS\" target=\"_top\">Click Here!</a>']")).Click();
            driver.FindElement(By.CssSelector("button.cssButton.taupeCssButton")).Click();
        }
        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }
        
        private string CloseAlertAndGetItsText() {
            try {
                IAlert alert = driver.SwitchTo().Alert();
                if (acceptNextAlert) {
                    alert.Accept();
                } else {
                    alert.Dismiss();
                }
                return alert.Text;
            } finally {
                acceptNextAlert = true;
            }
        }
    }
}
